(function(){

    //工具函数
    var global_id = {};
    /**
     * 创建全局唯一ID
     * @param {String} name id名
     */
    function create_id(name){
        var idname = "global";
        if(!(name == undefined || name == "")){
            idname = name;
        }
        global_id[idname] =  global_id[idname] + 1;
        return global_id[idname];
    }

    /**
     * 图层
     * 
     * 1 画布层 * 1 
     *      背景色，辅助线，标尺
     * 2 用户图层 * n
     * 3 文字标注层 * 1
     * 4 操作层
     * */  
    var layer = function(id,){
        
    };
    //图元组件
    var component = function(id, sprite, x, y, w, h){
        this.id = id;
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.sprite = sprite;    
        this.sprite.onMousePressed = function(){  
            //console.log("1-" + p5_sprite.myismove);                      
            if(sprite.myismove != true){                
                sprite.myismove = true;                
            }  
            //console.log("3-" + p5_sprite.myismove);
        };
        this.sprite.onMouseReleased = function(){                        
            if(sprite.myismove != false){
               sprite.myismove = false;                
            } 
            //console.log(p5_sprite);
        };           
    };

 
    component.prototype.update = function(){
        if(this.sprite.myismove){
            this.x = mouseX;
            this.y = mouseY;
            this.move(this.x, this.y);
        }
    }
    
    var p5help = window.p5help || {};    
    p5help.all_component = {}; //所有的图元
    p5help.rm_component = {};   //删除的图元 用于撤销删除操作
    
    p5help.is_number = function(obj){
        return obj.constructor == Number;
    };
    p5help.is_obj = function(obj, parentType){
        return obj.prototype.constructor == parentType;
    };
    var add_component = function(obj){
        p5help.all_component[obj.id] = obj;
    };
    p5help.create_rect = function(x, y, w, h){    
        var my_rect = function(){
            
        };  
        var p5_sprite = createSprite(x, y, w, h);            
        p5_sprite.draw = function(){     
            fill(220, 10, 10);
            push();
            rect(0, 0, w, h);
            pop();   
        }; 
         
        my_rect.prototype = new component(create_id(),p5_sprite, x, y, w, h);   
        my_rect.prototype.move = function(x, y){
            p5_sprite.position.x = x;
            p5_sprite.position.y = y;
        };
        var rtobj = new my_rect();
        add_component(rtobj);
        return rtobj;
    };
    var remove_by_id = function(id){
        var rmobj = p5help.all_component[id];
        if(rmobj != undefined && p5help.is_obj(rmobj, component)){
            p5help.rm_component[id] = rmobj;
            delete p5help.all_component[id];
        }        
    };
    p5help.remove = function(c1){
        if(p5help.is_number(c1)){
            remove_by_id(c1);
        }else if(p5help.is_obj(c1, component)){
            remove_by_id(c1.id);
        }
    };
    p5help.update = function(){
        for(var id in p5help.all_component){
            p5help.all_component[id].update();
        }
    };
    window.p5help = p5help;
}());